package com.chenjun.demo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by chenjun on 2017/1/9.
 */

public class CustomImageView extends ImageView {

    private BitmapRegionDecoder mDecoder;
    private int mBitmapWidth,mBitmapHeight;
    private int mWidth,mHeight;
    private int startX,startY,endX,endY;
    private BitmapFactory.Options options;
    private Rect mRect;
    private int dx,dy;

    public CustomImageView(Context context) {
        this(context,null);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
    }

    public void init(String pathName) {
        try {
            mDecoder = BitmapRegionDecoder.newInstance(pathName,false);
            BitmapFactory.decodeFile(pathName, options);
            getWidthAndHeight();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init(byte[] data){
        try {
            mDecoder = BitmapRegionDecoder.newInstance(data,0,data.length,false);
            BitmapFactory.decodeByteArray(data,0,data.length,options);
            getWidthAndHeight();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init(InputStream is){
        try {
            mDecoder = BitmapRegionDecoder.newInstance(is,false);
            BitmapFactory.decodeStream(is,null,options);
            getWidthAndHeight();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init(FileDescriptor fd){
        try {
            mDecoder = BitmapRegionDecoder.newInstance(fd,false);
            BitmapFactory.decodeFileDescriptor(fd,null,options);
            getWidthAndHeight();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getWidthAndHeight(){
        mBitmapHeight = options.outHeight;
        mBitmapWidth = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            startX = (int) event.getX();
            startY = (int) event.getY();
        }else if (event.getAction() == MotionEvent.ACTION_MOVE){
            endX = (int) event.getX();
            endY = (int) event.getY();
            checkWidth();
            checkHeight();
            invalidate();
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap mBitmap = mDecoder.decodeRegion(mRect,options);
        canvas.drawBitmap(mBitmap,0,0,null);
    }

    private void checkHeight() {
        dy = endY - startY;
        if (mRect.top + dy <= 0){
            mRect.top = 0;
        }else if (mRect.bottom + dy >= mBitmapHeight){
            mRect.bottom = mBitmapHeight;
        }else {
            mRect.top += dy;
            mRect.bottom += dy;
        }
    }

    private void checkWidth() {
        dx = endX - startX;
        if (mRect.left + dx <= 0){
            mRect.left = 0;
        }else if (mRect.right + dx >= mBitmapWidth){
            mRect.right = mBitmapWidth;
        }else {
            mRect.left += dy;
            mRect.right += dy;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        int imageWidth = mBitmapWidth;
        int imageHeight = mBitmapHeight;

        mRect = new Rect();
        mRect.left = imageWidth / 2 - mWidth / 2;
        mRect.right = mRect.left + mWidth;
        mRect.top = imageHeight / 2 - mHeight / 2;
        mRect.bottom = mRect.top + mHeight;

    }
}
